package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piggy.common.core.constant.SecurityConstants;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.logi.sys.bo.LogiSysSettingEditBo;
import com.piggy.logi.sys.bo.LogiSysSettingQueryBo;
import com.piggy.logi.sys.domain.LogiSysSetting;
import com.piggy.logi.sys.mapper.LogiSysSettingMapper;
import com.piggy.logi.sys.service.ILogiSysSettingService;
import com.piggy.logi.sys.vo.LogiSysSettingVo;
import com.piggy.sys.api.RemoteOssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 物流系统设置Service业务层处理
 *
 * @author piggy
 * @date 2023-11-13
 */
@Slf4j
@Service
public class LogiSysSettingServiceImpl extends ServiceImpl<LogiSysSettingMapper, LogiSysSetting> implements ILogiSysSettingService {

    @Resource
    private RemoteOssService ossService;

    @Override
    public LogiSysSettingVo queryById(Long id){
        return fillData(getVoById(id, LogiSysSettingVo.class));
    }

    @Override
    public TableDataInfo<LogiSysSettingVo> queryPageList(LogiSysSettingQueryBo bo) {
        PagePlus<LogiSysSetting, LogiSysSettingVo> result = pageVo(PageUtils.buildPagePlus(false), buildQueryWrapper(bo), LogiSysSettingVo.class);
        if (CollUtil.isNotEmpty(result.getRecordsVo())) {
            for (LogiSysSettingVo logiSysSettingVo : result.getRecordsVo()) {
                fillData(logiSysSettingVo);
            }
        }
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiSysSettingVo> queryList(LogiSysSettingQueryBo bo) {
        List<LogiSysSettingVo> voList = listVo(buildQueryWrapper(bo), LogiSysSettingVo.class);
        if (CollUtil.isNotEmpty(voList)) {
            for (LogiSysSettingVo logiSysSettingVo : voList) {
                fillData(logiSysSettingVo);
            }
        }
        return voList;
    }

    private LambdaQueryWrapper<LogiSysSetting> buildQueryWrapper(LogiSysSettingQueryBo bo) {
        LambdaQueryWrapper<LogiSysSetting> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, LogiSysSetting::getId, bo.getId());
        lqw.like(StrUtil.isNotBlank(bo.getLogiName()), LogiSysSetting::getLogiName, bo.getLogiName());
        lqw.eq(StrUtil.isNotBlank(bo.getVersion()), LogiSysSetting::getVersion, bo.getVersion());
        lqw.eq(StrUtil.isNotBlank(bo.getFilingNo()), LogiSysSetting::getFilingNo, bo.getFilingNo());
        lqw.eq(StrUtil.isNotBlank(bo.getCustomerTel1()), LogiSysSetting::getCustomerTel1, bo.getCustomerTel1());
        lqw.eq(StrUtil.isNotBlank(bo.getCustomerTel2()), LogiSysSetting::getCustomerTel2, bo.getCustomerTel2());
        lqw.eq(StrUtil.isNotBlank(bo.getLoginPc()), LogiSysSetting::getLoginPc, bo.getLoginPc());
        lqw.eq(StrUtil.isNotBlank(bo.getLoginMobile()), LogiSysSetting::getLoginMobile, bo.getLoginMobile());
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiSysSettingVo insertByAddBo(LogiSysSettingEditBo bo) {
        LogiSysSetting add = BeanUtil.toBean(bo, LogiSysSetting.class);
        save(add);
        return queryById(add.getId());
    }

    @Override
    @Transactional
    public LogiSysSettingVo updateByEditBo(LogiSysSettingEditBo bo) {
        LogiSysSetting update = BeanUtil.toBean(bo, LogiSysSetting.class);
        updateById(update);
        return queryById(update.getId());
    }

    @Override
    @Transactional
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    private LogiSysSettingVo fillData(LogiSysSettingVo vo) {
        if (ObjectUtil.isEmpty(vo)) {
            return vo;
        }
        if (StrUtil.isNotBlank(vo.getLoginPc())) {
            vo.setLoginPcUrl(ossService.getSysOssById(vo.getLoginPc(), SecurityConstants.INNER).getData());
        }
        if (StrUtil.isNotBlank(vo.getLoginMobile())) {
            vo.setLoginMobileUrl(ossService.getSysOssById(vo.getLoginMobile(), SecurityConstants.INNER).getData());
        }
        return vo;
    }

}
